package com.jumi.microservice.controller.logistics.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.domain.GoodsInfoDTO;
import com.jumi.microservice.dto.logistics.AddOrUpdateSpiltMergeSetupRequest;
import com.jumi.microservice.dto.logistics.DelSpiltMergeSetupRequest;
import com.jumi.microservice.dto.logistics.ListSpiltMergeSetupRequest;
import com.jumi.microservice.dto.logistics.ListSpiltMergeSetupResponse;
import com.jumi.microservice.dto.logistics.excel.SpiltMergeSetupInfo;
import com.jumi.microservice.service.logistics.admin.SpiltMergeSetupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author by mazhao
 * @Classname SpiltMergeSetupController
 * @Description TODO
 * @Date 2020/8/4 10:25
 */
@Api(tags = {"拆合单规则相关接口"})
@RestController
@RequestMapping("/admin/logistics")
public class SpiltMergeSetupController extends BaseController {

    @Resource
    SpiltMergeSetupService spiltMergeSetupService;
    /**
     * 添加拆合单规则
     *
     * @param request
     * @return
     */
    @ApiOperation("添加拆合单规则")
    @PostMapping("/spiltMergeSetup/add")
    public ResponseResult<Boolean> add(AddOrUpdateSpiltMergeSetupRequest request) {
        request.setAdminIdAdd(getUid());
        request.setAdminNameAdd(getUserName());
        return success(spiltMergeSetupService.add(request));
    }

    /**
     * 修改拆合单规则
     *
     * @param request
     * @return
     */
    @ApiOperation("修改拆合单规则")
    @PostMapping("/spiltMergeSetup/update")
    public ResponseResult<Boolean> update(AddOrUpdateSpiltMergeSetupRequest request) {
        request.setAdminIdUpdate(getUid());
        request.setAdminNameUpdate(getUserName());
        return success(spiltMergeSetupService.update(request));
    }

    /**
     * 删除拆合单规则
     *
     * @param request
     * @return
     */
    @ApiOperation("删除拆合单规则")
    @PostMapping("/spiltMergeSetup/del")
    public ResponseResult<Boolean> del(DelSpiltMergeSetupRequest request) {
        return success(spiltMergeSetupService.del(request));
    }

    /**
     * 拆合单列表
     *
     * @param request
     * @return
     */
    @ApiOperation("拆合单规则列表")
    @PostMapping("/spiltMergeSetup/list")
    public TableDataInfo<ListSpiltMergeSetupResponse> list(ListSpiltMergeSetupRequest request) {
        return spiltMergeSetupService.list(request);
    }

    /**
     * SPU 搜索列表
     *
     * @param request
     * @return
     */
    @ApiOperation("SPU搜索列表")
    @PostMapping("/spiltMergeSetup/spuSearchList")
    public TableDataInfo<GoodsInfoDTO> spuSearchList(ListSpiltMergeSetupRequest request) {
        return spiltMergeSetupService.spuSearchList(request);
    }

    /**
     * 导出
     */
    @ApiOperation("SPU规则列表导出")
    @PostMapping("/spiltMergeSetup/export")
    public void export(HttpServletResponse response,ListSpiltMergeSetupRequest request) throws IOException {
        ExportExcelCommonResponse<SpiltMergeSetupInfo> exportUserCashResponseExportExcelCommonResponse = spiltMergeSetupService.excelExport(request);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+ exportUserCashResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), SpiltMergeSetupInfo.class).sheet("sheet1").doWrite(exportUserCashResponseExportExcelCommonResponse.getData());
    }
}
